﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Windows;

namespace AGameOfThrones.game
{
    public class house : INotifyPropertyChanged, stream.streamable
    {
        public event PropertyChangedEventHandler PropertyChanged;  
        
        public house()
        {
        }

        protected void Notify(string property)
        {
            if(this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public game Game 
        { 
            get 
            {
                try
                {
                    return Application.Current.FindResource("gamestate") as game;
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
                
            }
        }


        public void Initialize()
        {
            foreach (piece p in Units)
            {
                p.Initialize();
            }
        }


#region House Props

        public Color housecolor;
        public Color HouseColor
        {
            get { return housecolor; }
            set
            {
                if (housecolor != value)
                {
                    housecolor = value;
                    Notify("HouseColor");
                }
            }
        }

        public String housename;
        public String HouseName
        {
            get { return housename; }
            set
            {
                if (housename != value)
                {
                    housename = value;
                    Notify("HouseName");
                }
            }
        }

#endregion
        
#region Power Tokens 

        public ImageSource powertokenimage;
        public ImageSource PowerTokenImage
        {
            get { return powertokenimage; }
            set
            {
                if (powertokenimage != value)
                {
                    powertokenimage = value;
                    Notify("PowerTokenImage");
                }
            }
        }

        public ObservableCollection<powertoken> powertokens = new ObservableCollection<powertoken>();
        public ObservableCollection<powertoken> PowerTokens
        {
            get { return powertokens; }
            set
            {
                if (powertokens != value)
                {
                    powertokens = value;
                    Notify("PowerTokens");
                }
            }
        }

        public ObservableCollection<powertoken> powertokensavailable = new ObservableCollection<powertoken>();
        public ObservableCollection<powertoken> PowerTokensAvailable
        {
            get { return powertokensavailable; }
            set
            {
                if (powertokensavailable != value)
                {
                    powertokensavailable = value;
                    Notify("PowerTokensAvailable");
                }
            }
        }

#endregion


#region Special Tokens

        public ObservableCollection<specialtoken> specialtokens = new ObservableCollection<specialtoken>();
        public ObservableCollection<specialtoken> SpecialTokens
        {
            get { return specialtokens; }
            set
            {
                if (specialtokens != value)
                {
                    specialtokens = value;
                    Notify("SpecialTokens");
                }
            }
        }

        #endregion

#region Order Tokens

        public ImageSource ordertokenimage;
        public ImageSource OrderTokenImage
        {
            get { return ordertokenimage; }
            set
            {
                if (ordertokenimage != value)
                {
                    ordertokenimage = value;
                    Notify("OrderTokenImage");
                }
            }
        }

        public ObservableCollection<ordertoken> ordertokens = new ObservableCollection<ordertoken>();
        public ObservableCollection<ordertoken> OrderTokens
        {
            get { return ordertokens; }
            set
            {
                if (ordertokens != value)
                {
                    ordertokens = value;
                    Notify("OrderTokens");
                }
            }
        }

#endregion
        
#region Units 

        public ObservableCollection<unit> units = new ObservableCollection<unit>();
        public ObservableCollection<unit> Units
        {
            get { return units; }
            set
            {
                if (units != value)
                {
                    units = value;
                    Notify("Units");
                }
            }
        }

#endregion


        public void serialize(stream.stream io_Stream)
        {
            io_Stream.begin(HouseName)
                .serialize("PowerTokens", ref powertokens)
                .serialize("SpecialTokens", ref specialtokens)
                .serialize("OrderTokens", ref ordertokens)
                .serialize("Units", ref units)                
            .end();
        }
    }
}
